package info.javafans.servlet;

import info.javafans.dao.CategoryDao;
import info.javafans.dao.CommentDao;
import info.javafans.dao.PostDao;
import info.javafans.domain.Category;
import info.javafans.domain.Comment;
import info.javafans.domain.Post;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShowCatetory
 */
public class ShowPostsByCategory extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowPostsByCategory() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		
		//获取文章列表
		PostDao postDao = new PostDao();
		List<Post> list = postDao.selectPostByCategory(id);
		request.setAttribute("postList", list);
		
		//获取分类列表--common
		CategoryDao categoryDao = new CategoryDao();
		List<Category> categoryList = categoryDao.selectAllCategory();
		request.setAttribute("categoryList", categoryList);
		
		//获得viewCount排名文章列表--common
		List<Post> viewRankList = postDao.selectMostViewTitle();
		request.setAttribute("viewRankList", viewRankList);
		
		//获得评论列表--common
		CommentDao commentDao = new CommentDao();
		List<Comment> newCommentList = commentDao.selectNewComments();
		request.setAttribute("newCommentList", newCommentList);
		
		//当前分类
		Category category = categoryDao.selectCategoryById(id);
		request.setAttribute("theCategory", category);
		
		//获得最新文章标题列表--common
		List<Post> newPostTitleList = postDao.selectNewPostTitle();
		request.setAttribute("newPostTitleList", newPostTitleList);
		
		//Flag
		request.setAttribute("flag", "category");
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

}
